<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是进程的基础知识相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV05-01-进程-01-进程的基础知识">
<meta property="og:url" content="https://sumumm.github.io/post/d2eb46ea.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是进程的基础知识相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20220521065909591.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20220521105531500.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20220521112107420.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20220521112329080.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20220523214829930.png">
<meta property="article:published_time" content="2023-07-02T13:51:51.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.992Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV05-操作系统">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20220521065909591.png">


<link rel="canonical" href="https://sumumm.github.io/post/d2eb46ea.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/d2eb46ea.html","path":"post/d2eb46ea.html","title":"LV05-01-进程-01-进程的基础知识"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV05-01-进程-01-进程的基础知识 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E8%BF%9B%E7%A8%8B%E4%B8%8E%E7%A8%8B%E5%BA%8F"><span class="nav-text">一、进程与程序</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="nav-text">1. 基本概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%A8%8B%E5%BA%8F%E7%9A%84%E7%BB%93%E6%9D%9F"><span class="nav-text">2. 程序的结束</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%87%A0%E4%B8%AA%E5%87%BD%E6%95%B0"><span class="nav-text">3. 几个函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-abort"><span class="nav-text">3.1 abort() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">3.1.1 函数说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.1.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-atexit"><span class="nav-text">3.2 atexit() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">3.2.1 函数说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.2.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-sleep"><span class="nav-text">3.3 sleep() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">3.3.1 函数说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.3.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-usleep"><span class="nav-text">3.4 usleep() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">3.4.1 函数说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.4.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%B8%B8%E7%94%A8-Shell-%E5%91%BD%E4%BB%A4"><span class="nav-text">4. 常用 Shell 命令</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E8%BF%9B%E7%A8%8B%E7%9A%84ID"><span class="nav-text">二、进程的ID </span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E8%BF%9B%E7%A8%8BID"><span class="nav-text">1. 进程ID</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E8%8E%B7%E5%8F%96%E8%BF%9B%E7%A8%8BID"><span class="nav-text">2. 获取进程ID</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-getpid"><span class="nav-text">2.1 getpid() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">2.1.1 函数说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.1.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-getppid"><span class="nav-text">2.2 getppid() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">2.2.1 函数说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.2.2 使用实例</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E8%BF%9B%E7%A8%8B%E7%9A%84%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><span class="nav-text">三、进程的环境变量</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-shell-%E5%91%BD%E4%BB%A4"><span class="nav-text">1. shell 命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E4%BD%9C%E7%94%A8"><span class="nav-text">2. 环境变量作用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E8%8E%B7%E5%8F%96%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><span class="nav-text">3. 获取环境变量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-environ"><span class="nav-text">3.1 environ </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-1-%E5%8F%98%E9%87%8F%E8%AF%B4%E6%98%8E"><span class="nav-text">3.1.1 变量说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-1"><span class="nav-text">3.1.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-getenv"><span class="nav-text">3.2 getenv() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E-1"><span class="nav-text">3.2.1 函数说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-1"><span class="nav-text">3.2.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E6%B8%85%E7%A9%BA%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><span class="nav-text">4. 清空环境变量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-environ-%E7%BD%AE-NULL"><span class="nav-text">4.1 environ 置 NULL </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-clearenv"><span class="nav-text">4.2 clearenv() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">4.2.1 函数说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.2.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E5%A2%9E%E5%88%A0%E6%94%B9"><span class="nav-text">5. 增删改</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-putenv"><span class="nav-text">5.1 putenv() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-1-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">5.1.1 函数说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-1-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">5.1.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-setenv"><span class="nav-text">5.2 setenv() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">5.2.1 函数说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">5.2.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-unsetenv"><span class="nav-text">5.3 unsetenv() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-3-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">5.3.1 函数说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-3-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">5.3.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-4-%E7%9B%B4%E6%8E%A5%E6%B7%BB%E5%8A%A0"><span class="nav-text">5.4 直接添加</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-4-1-%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">5.4.1 使用格式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-4-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">5.4.2 使用实例</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%86%85%E5%AD%98%E5%B8%83%E5%B1%80"><span class="nav-text">四、进程的内存布局</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E8%BF%9B%E7%A8%8B%E7%9A%84%E7%B1%BB%E5%9E%8B%E4%B8%8E%E7%8A%B6%E6%80%81"><span class="nav-text">五、进程的类型与状态</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E8%BF%9B%E7%A8%8B%E7%9A%84%E7%B1%BB%E5%9E%8B"><span class="nav-text">1. 进程的类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E8%BF%9B%E7%A8%8B%E7%9A%84%E7%8A%B6%E6%80%81"><span class="nav-text">2. 进程的状态</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81%E8%BF%9B%E7%A8%8B%E7%9A%84%E8%99%9A%E6%8B%9F%E5%9C%B0%E5%9D%80"><span class="nav-text">六、进程的虚拟地址</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%83%E3%80%81%E8%BF%9B%E7%A8%8B%E7%9A%84%E4%B8%8A%E4%B8%8B%E6%96%87%E5%88%87%E6%8D%A2"><span class="nav-text">七、进程的上下文切换</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BB%80%E4%B9%88%E6%98%AF%E4%B8%8A%E4%B8%8B%E6%96%87%E5%88%87%E6%8D%A2%EF%BC%9F"><span class="nav-text">1. 什么是上下文切换？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%B8%8A%E4%B8%8B%E6%96%87%E5%88%87%E6%8D%A2%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="nav-text">2. 上下文切换什么？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%BB%80%E4%B9%88%E5%9C%BA%E6%99%AF%E4%BC%9A%E5%8F%91%E7%94%9F%EF%BC%9F"><span class="nav-text">3. 什么场景会发生？</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/d2eb46ea.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV05-01-进程-01-进程的基础知识 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV05-01-进程-01-进程的基础知识
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-02 21:51:51" itemprop="dateCreated datePublished" datetime="2023-07-02T21:51:51+08:00">2023-07-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">LV05-操作系统</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>9.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>35 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是进程的基础知识相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150>---</td>        <td align="left"><a href="" target="_blank">---<i class="fa fa-external-link-alt"></i> </a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、进程与程序"><a href="#一、进程与程序" class="headerlink" title="一、进程与程序"></a><font size=3>一、进程与程序</font></h1><h2 id="1-基本概念"><a href="#1-基本概念" class="headerlink" title="1. 基本概念"></a><font size=3>1. 基本概念</font></h2><table>
    <tr><td align="center" width=100px>程序  </td><td align="left">程序是存放在磁盘上的指令和数据的有序集合（文件），是静态的文件。</td></tr>
    <tr><td align="center" width=100px>进程  </td><td align="left">进程是执行一个程序所分配的资源的总称，它是程序的一次执行过程，是动态的，包括创建、调度、执行和消亡等。当程序被加载到内存中运行之后它就称为了一个进程，当程序运行结束后也就意味着进程终止，这就是进程的一个生命周期。</td></tr>
    <tr><td align="center" width=100px>进程号</td><td align="left">Linux系统下的每一个进程都有一个进程号（process ID，简称PID），进程号是一个正数，用于唯一标识系统中的某一个进程。</td></tr>
    <tr><td align="center" width=100px>主进程</td><td align="left">程序执行的入口，我们自己编写的C语言程序中，一般是main函数。</td></tr>
    <tr><td align="center" width=100px>父进程</td><td align="left">已经创建了一个或者多个子进程的进程， 任何进程都有父进程，追根溯源是系统启动程序。我们一般写的程序，主进程是最初始的父进程。</td></tr>
    <tr><td align="center" width=100px>子进程</td><td align="left">父进程创建的进程，一个子进程只能对应一个父进程。</td></tr>
</table>

<h2 id="2-程序的结束"><a href="#2-程序的结束" class="headerlink" title="2. 程序的结束"></a><font size=3>2. 程序的结束</font></h2><p>我们都知道 C 语言程序总是从 main 函数开始执行，但它是怎么结束的呢？程序结束其实就是进程终止，进程终止的方式通常有多种，大体上分为<strong>正常终止</strong>和<strong>异常终止</strong>。</p>
<div class="tabs" id="tabname1"><ul class="nav-tabs"><li class="tab active"><a href="#tabname1-1">正常终止</a></li><li class="tab"><a href="#tabname1-2">异常终止</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname1-1"><p>前边的三种较为常见，后边两种暂时还未使用过，也还未见过。</p>
<p>（1） main() 函数中通过 return 语句返回来终止进程。</p>
<p>（2）程序中调用 exit() 函数终止进程。</p>
<p>（3）程序中调用 _exit() 或 _Exit() 终止进程。</p>
<p>（4）最后一个线程从其启动例程返回。</p>
<p>（5）最后一个线程调用 pthread_exit 。</p></div><div class="tab-pane" id="tabname1-2"><p>（1）应用程序中调用 abort() 函数终止进程。</p>
<p>（2）进程接收到一个信号，譬如 SIGKILL 信号。</p>
<p>（3）最后一个线程对取消请求做出响应。</p></div></div></div>

<h2 id="3-几个函数"><a href="#3-几个函数" class="headerlink" title="3. 几个函数"></a><font size=3>3. 几个函数</font></h2><h3 id="3-1-abort"><a href="#3-1-abort" class="headerlink" title="3.1 abort() "></a><font size=3>3.1 abort() </font></h3><h4 id="3-1-1-函数说明"><a href="#3-1-1-函数说明" class="headerlink" title="3.1.1 函数说明"></a><font size=3>3.1.1 函数说明</font></h4><p>在 linux 下可以使用 man abort 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">void</span> <span class="title function_">abort</span><span class="params">(<span class="type">void</span>)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数使程序异常终止，会直接从调用的地方跳出。</p>
<p><strong>【函数参数】</strong> none </p>
<p><strong>【返回值】</strong> none </p>
<p><strong>【注意事项】</strong></p>
<p>（1）函数首先解除 SIGABRT 信号的阻塞，然后为调用进程发出该信号，就像调用了 raise (后边会学习到)，这将导致进程异常终止，除非捕获 SIGABRT 信号并且信号处理程序不返回。</p>
<p>（2）使用 abort() 终止进程运行，会生成核心转储文件，可用于判断程序调用 abort() 时的程序状态。</p>
<h4 id="3-1-2-使用实例"><a href="#3-1-2-使用实例" class="headerlink" title="3.1.2 使用实例"></a><font size=3>3.1.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="built_in">printf</span>(<span class="string">&quot;before abort!\n&quot;</span>);</span><br><span class="line">   <span class="built_in">abort</span>();</span><br><span class="line">   <span class="built_in">printf</span>(<span class="string">&quot;after abort!\n&quot;</span>);</span><br><span class="line">   <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">before abort!</span><br><span class="line">已放弃 (核心已转储)</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-2-atexit"><a href="#3-2-atexit" class="headerlink" title="3.2 atexit() "></a><font size=3>3.2 atexit() </font></h3><h4 id="3-2-1-函数说明"><a href="#3-2-1-函数说明" class="headerlink" title="3.2.1 函数说明"></a><font size=3>3.2.1 函数说明</font></h4><p>在 linux 下可以使用 man atexit 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">atexit</span><span class="params">(<span class="type">void</span> (*function)(<span class="type">void</span>))</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会在程序正常终止时，调用指定的函数  function ，该函数也可以被称之为登记函数。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>function ：在程序终止时被调用的函数。</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看参数解读 </summary>
              <div class='content'>
              <ul><li>*function 表示定义了一个指针变量 function ；</li><li>后边的括号中的 void 表示这个指针变量可以指向一个没有参数的函数；</li><li>前边的 void 表示这个指针变量可以指向返回值类型为 void 的函数。</li></ul><p>总的来说，就是定义了一个指向无形参无返回值的函数的指针变量 function ，它可以指向符合条件的函数。</p>
              </div>
            </details>

<p><strong>【返回值】</strong>如果函数成功注册，则该函数返回 0 ，否则返回一个非 0 值。</p>
<p><strong>【注意事项】</strong></p>
<p>（1）我们可以在任何地方注册终止函数，它会在程序终止的时候被调用。</p>
<p>（2）后注册的函数在程序正常结束时将会被先调用。</p>
<p>（3）如果⼀个函数被多次注册，也会被多次调用。</p>
<p>（4）此函数可以用于注册一些清理工作的函数，例如清理内存等。</p>
<h4 id="3-2-2-使用实例"><a href="#3-2-2-使用实例" class="headerlink" title="3.2.2 使用实例"></a><font size=3>3.2.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">func1</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;This is fun1!\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">void</span> <span class="title function_">func2</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;This is fun2!\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;before atexit!\n&quot;</span>);</span><br><span class="line">	atexit(func1);</span><br><span class="line">	atexit(func2);</span><br><span class="line">    atexit(func1);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;after atexit!\n&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">before atexit!</span><br><span class="line">after atexit!</span><br><span class="line">This is fun1!</span><br><span class="line">This is fun2!</span><br><span class="line">This is fun1!</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-3-sleep"><a href="#3-3-sleep" class="headerlink" title="3.3 sleep() "></a><font size=3>3.3 sleep() </font></h3><h4 id="3-3-1-函数说明"><a href="#3-3-1-函数说明" class="headerlink" title="3.3.1 函数说明"></a><font size=3>3.3.1 函数说明</font></h4><p>在 linux 下可以使用 man 3 sleep 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">int</span> <span class="title function_">sleep</span><span class="params">(<span class="type">unsigned</span> <span class="type">int</span> seconds)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会使进程休眠，此时该进程不占用 CPU 资源。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>seconds ： unsigned int 类型，程序休眠的时间，以 s （秒）为单位。</li>
</ul>
<p><strong>【返回值】</strong>返回值为 unsigned int 类型，若进程或者线程挂起到参数所指定的时间则返回 0 ，若有信号中断则返回剩余秒数。</p>
<p><strong>【注意事项】</strong> none </p>
<h4 id="3-3-2-使用实例"><a href="#3-3-2-使用实例" class="headerlink" title="3.3.2 使用实例"></a><font size=3>3.3.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;i = %d!\n&quot;</span>, i++);</span><br><span class="line">		sleep(<span class="number">1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会每隔 1s 输出一次 i 的值：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">i = 0!</span><br><span class="line">i = 1!</span><br><span class="line">i = 2!</span><br><span class="line">i = 3!</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-4-usleep"><a href="#3-4-usleep" class="headerlink" title="3.4 usleep() "></a><font size=3>3.4 usleep() </font></h3><h4 id="3-4-1-函数说明"><a href="#3-4-1-函数说明" class="headerlink" title="3.4.1 函数说明"></a><font size=3>3.4.1 函数说明</font></h4><p>在 linux 下可以使用 man 3 usleep 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">usleep</span><span class="params">(<span class="type">useconds_t</span> usec)</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会使进程休眠，此时该进程不占用 CPU 资源。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>usec ： useconds_t 类型，程序休眠的时间，以 us （微秒）为单位。</li>
</ul>
<p><strong>【返回值】</strong>返回值为 int 类型，若进程或者线程挂起到参数所指定的时间则返回 0 ，若有信号中断则返回剩余秒数。</p>
<p><strong>【注意事项】</strong> 1s &#x3D; 1000ms &#x3D; 1000 000us </p>
<h4 id="3-4-2-使用实例"><a href="#3-4-2-使用实例" class="headerlink" title="3.4.2 使用实例"></a><font size=3>3.4.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;i = %d!\n&quot;</span>, i++);</span><br><span class="line">		usleep(<span class="number">2000</span>*<span class="number">1000</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会每隔 2s 输出一次 i 的值：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">i = 0!</span><br><span class="line">i = 1!</span><br><span class="line">i = 2!</span><br><span class="line">i = 3!</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="4-常用-Shell-命令"><a href="#4-常用-Shell-命令" class="headerlink" title="4. 常用 Shell 命令"></a><font size=3>4. 常用 Shell 命令</font></h2><p>这一部分可以查看前边学习shell的时候的相关命令</p>
<h1 id="二、进程的ID"><a href="#二、进程的ID" class="headerlink" title="二、进程的ID "></a><font size=3>二、进程的ID </font></h1><h2 id="1-进程ID"><a href="#1-进程ID" class="headerlink" title="1. 进程ID"></a><font size=3>1. 进程ID</font></h2><p>在维基百科中是这样说的：在计算机领域，进程标识符（英语：process identifier，又略称为进程ID（英语：process ID）、PID）是大多数操作系统的内核用于唯一标识进程的一个数值。这一数值可以作为许多函数调用的参数，以使调整进程优先级、杀死进程之类的进程控制行为成为可能。</p>
<h2 id="2-获取进程ID"><a href="#2-获取进程ID" class="headerlink" title="2. 获取进程ID"></a><font size=3>2. 获取进程ID</font></h2><h3 id="2-1-getpid"><a href="#2-1-getpid" class="headerlink" title="2.1 getpid() "></a><font size=3>2.1 getpid() </font></h3><h4 id="2-1-1-函数说明"><a href="#2-1-1-函数说明" class="headerlink" title="2.1.1 函数说明"></a><font size=3>2.1.1 函数说明</font></h4><p>在 linux 下可以使用 man getpid 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="type">pid_t</span> <span class="title function_">getpid</span><span class="params">(<span class="type">void</span>)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会获取当前进程的进程号，也就是进程的 PID 。</p>
<p><strong>【函数参数】</strong> none </p>
<p><strong>【返回值】</strong>返回值为 pid_t 类型，表示当前进程的进程号。</p>
<p><strong>【注意事项】</strong> none </p>
<h4 id="2-1-2-使用实例"><a href="#2-1-2-使用实例" class="headerlink" title="2.1.2 使用实例"></a><font size=3>2.1.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">pid_t</span> pid;</span><br><span class="line">	pid = getpid();</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;This process PID: %d\n&quot;</span>, pid);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">This process PID: 3877</span><br></pre></td></tr></table></figure><p>每次运行获得的 PID 可能会不同。</p>
              </div>
            </details>

<h3 id="2-2-getppid"><a href="#2-2-getppid" class="headerlink" title="2.2 getppid() "></a><font size=3>2.2 getppid() </font></h3><h4 id="2-2-1-函数说明"><a href="#2-2-1-函数说明" class="headerlink" title="2.2.1 函数说明"></a><font size=3>2.2.1 函数说明</font></h4><p>在 linux 下可以使用 man getppid 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="type">pid_t</span> <span class="title function_">getppid</span><span class="params">(<span class="type">void</span>)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会获取当前进程父进程的进程号。</p>
<p><strong>【函数参数】</strong> none </p>
<p><strong>【返回值】</strong>返回值为 pid_t 类型，表示当前进程父进程的进程号。</p>
<p><strong>【注意事项】</strong> none </p>
<h4 id="2-2-2-使用实例"><a href="#2-2-2-使用实例" class="headerlink" title="2.2.2 使用实例"></a><font size=3>2.2.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">pid_t</span> pid;</span><br><span class="line">	pid = getppid();</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;This process PID: %d\n&quot;</span>, pid);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">This process&#x27;s father PID: 1813</span><br></pre></td></tr></table></figure><p>每次运行获得的 PID 可能会不同。</p>
              </div>
            </details>

<h1 id="三、进程的环境变量"><a href="#三、进程的环境变量" class="headerlink" title="三、进程的环境变量"></a><font size=3>三、进程的环境变量</font></h1><p>每一个进程都有一组与其相关的环境变量，这些环境变量以字符串形式存储在一个字符串数组列表中，把这个数组称为<strong>环境列表</strong>。每个字符串都是以名称 &#x3D; 值（ name&#x3D;value ）形式定义。</p>
<h2 id="1-shell-命令"><a href="#1-shell-命令" class="headerlink" title="1. shell 命令"></a><font size=3>1. shell 命令</font></h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">env # 查看shell进程的所有环境变量</span><br><span class="line">export LINUX_APP=123456 # 添加LINUX_APP环境变量</span><br><span class="line">export -n LINUX_APP     # 删除LINUX_APP环境变量</span><br></pre></td></tr></table></figure>

<h2 id="2-环境变量作用"><a href="#2-环境变量作用" class="headerlink" title="2. 环境变量作用"></a><font size=3>2. 环境变量作用</font></h2><p>环境变量常见的用途之一是在 shell 中，每一个环境变量都有它所表示的含义，例如 HOME 环境变量表示用户的家目录， USER 环境变量表示当前用户名， SHELL 环境变量表示 shell 解析器名称， PWD 环境变量表示当前所在目录等，在自己的应用程序当中，也可以使用进程的环境变量。</p>
<h2 id="3-获取环境变量"><a href="#3-获取环境变量" class="headerlink" title="3. 获取环境变量"></a><font size=3>3. 获取环境变量</font></h2><h3 id="3-1-environ"><a href="#3-1-environ" class="headerlink" title="3.1 environ "></a><font size=3>3.1 environ </font></h3><h4 id="3-1-1-变量说明"><a href="#3-1-1-变量说明" class="headerlink" title="3.1.1 变量说明"></a><font size=3>3.1.1 变量说明</font></h4><p>在程序当中也可以获取当前进程的环境变量，进程的环境变量是从其父进程中继承过来的，例如在 shell 终端下执行一个应用程序，那么该进程的环境变量就是从其父进程（ shell 进程）中继承过来的。<br>环境变量存放在一个字符串数组中，在应用程序中，通过 environ 变量指向它， environ 是一个全局变量，在我们的程序中只需申明它就使用.</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">extern</span> <span class="type">char</span> **environ; <span class="comment">/* 声明外部全局变量environ */</span></span><br></pre></td></tr></table></figure>

<h4 id="3-1-2-使用实例-1"><a href="#3-1-2-使用实例-1" class="headerlink" title="3.1.2 使用实例"></a><font size=3>3.1.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="type">char</span> **environ;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; environ[i] != <span class="literal">NULL</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">puts</span>(environ[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">SHELL=/bin/bash</span><br><span class="line">SESSION_MANAGER=local/vm:@/tmp/.ICE-unix/1237,unix/vm:/tmp/.ICE-unix/1237</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">此处省略......</span></span><br><span class="line">DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus</span><br><span class="line">_=./a.out</span><br><span class="line">OLDPWD=/home/hk/2Sharedfiles/2Linux/01Study/05-LV5</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-2-getenv"><a href="#3-2-getenv" class="headerlink" title="3.2 getenv() "></a><font size=3>3.2 getenv() </font></h3><h4 id="3-2-1-函数说明-1"><a href="#3-2-1-函数说明-1" class="headerlink" title="3.2.1 函数说明"></a><font size=3>3.2.1 函数说明</font></h4><p>在 linux 下可以使用 man getenv 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">char</span> *<span class="title function_">getenv</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *name)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会获取指定的环境变量。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>name ：字符指针类型，指向指定要获取的 name&#x3D;value 形式的字符串的 name 。</li>
</ul>
<p><strong>【返回值】</strong>返回值为 char * 类型，如果存在环境变量，则返回该环境变量的值对应字符串的指针；如果不存在，则返回 NULL 。</p>
<p><strong>【注意事项】</strong> none </p>
<h4 id="3-2-2-使用实例-1"><a href="#3-2-2-使用实例-1" class="headerlink" title="3.2.2 使用实例"></a><font size=3>3.2.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> *envValue = <span class="literal">NULL</span>;</span><br><span class="line">	envValue = getenv(<span class="string">&quot;PATH&quot;</span>);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">puts</span>(envValue);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="4-清空环境变量"><a href="#4-清空环境变量" class="headerlink" title="4. 清空环境变量"></a><font size=3>4. 清空环境变量</font></h2><h3 id="4-1-environ-置-NULL"><a href="#4-1-environ-置-NULL" class="headerlink" title="4.1 environ 置 NULL "></a><font size=3>4.1 environ 置 NULL </font></h3><p>可以通过将全局变量 environ 赋值为 NULL 来清空所有变量。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">extern</span> <span class="type">char</span> **environ; <span class="comment">/* 声明外部全局变量environ */</span></span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="type">char</span> **environ;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; environ[i] != <span class="literal">NULL</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">puts</span>(environ[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;=============Clean up!==============\n&quot;</span>);</span><br><span class="line">	environ = <span class="literal">NULL</span>;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; environ[i] != <span class="literal">NULL</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">puts</span>(environ[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">SHELL=/bin/bash</span><br><span class="line">SESSION_MANAGER=local/vm:@/tmp/.ICE-unix/1237,unix/vm:/tmp/.ICE-unix/1237</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">此处省略......</span></span><br><span class="line">DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus</span><br><span class="line">_=./a.out</span><br><span class="line">OLDPWD=/home/hk/2Sharedfiles/2Linux/01Study/05-LV5</span><br><span class="line">=============Clean up!==============</span><br><span class="line">段错误 (核心已转储)</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="4-2-clearenv"><a href="#4-2-clearenv" class="headerlink" title="4.2 clearenv() "></a><font size=3>4.2 clearenv() </font></h3><h4 id="4-2-1-函数说明"><a href="#4-2-1-函数说明" class="headerlink" title="4.2.1 函数说明"></a><font size=3>4.2.1 函数说明</font></h4><p>在 linux 下可以使用 man clearenv 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">clearenv</span><span class="params">(<span class="type">void</span>)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会清除所有环境表中的变量。</p>
<p><strong>【函数参数】</strong> none </p>
<p><strong>【返回值】</strong>返回值为 int 类型，清空成功返回 0 ，否则返回 -1 。</p>
<p><strong>【注意事项】</strong>只能影响到操作进程和子进程，对其父进程没有影响。</p>
<h4 id="4-2-2-使用实例"><a href="#4-2-2-使用实例" class="headerlink" title="4.2.2 使用实例"></a><font size=3>4.2.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="type">char</span> **environ;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; environ[i] != <span class="literal">NULL</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">puts</span>(environ[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;=============Clean up!==============\n&quot;</span>);</span><br><span class="line">	ret = clearenv();</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;ret = %d\n&quot;</span>, ret);</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; environ[i] != <span class="literal">NULL</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">puts</span>(environ[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">SHELL=/bin/bash</span><br><span class="line">SESSION_MANAGER=local/vm:@/tmp/.ICE-unix/1237,unix/vm:/tmp/.ICE-unix/1237</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">此处省略......</span></span><br><span class="line">DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus</span><br><span class="line">_=./a.out</span><br><span class="line">OLDPWD=/home/hk/2Sharedfiles/2Linux/01Study/05-LV5</span><br><span class="line">=============Clean up!==============</span><br><span class="line">ret = 0</span><br><span class="line">段错误 (核心已转储)</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="5-增删改"><a href="#5-增删改" class="headerlink" title="5. 增删改"></a><font size=3>5. 增删改</font></h2><h3 id="5-1-putenv"><a href="#5-1-putenv" class="headerlink" title="5.1 putenv() "></a><font size=3>5.1 putenv() </font></h3><h4 id="5-1-1-函数说明"><a href="#5-1-1-函数说明" class="headerlink" title="5.1.1 函数说明"></a><font size=3>5.1.1 函数说明</font></h4><p>在 linux 下可以使用 man putenv 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">putenv</span><span class="params">(<span class="type">char</span> *<span class="built_in">string</span>)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会向当前进程的环境变量数组中添加一个新的环境变量，或者修改一个已经存在的环境变量对应的值。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>string ：字符指针类型，指向 name&#x3D;value 形式的字符串。</li>
</ul>
<p><strong>【返回值】</strong>返回值为 int 类型，成功返回 0 ；失败将返回非 0 值，并设置 errno 。</p>
<p><strong>【注意事项】</strong></p>
<p>（1）该函数调用成功之后，参数 string 所指向的字符串就成为了进程环境变量的一部分了，也就是说 putenv() 函数将设定 environ 变量（字符串数组）中的某个元素（字符串指针）指向该 string 字符串，而不是指向它的复制副本。因此不能随意修改参数 string 所指向的内容，这将影响进程的环境变量，另外参数 string 不应为自动变量（即在栈中分配的字符数组）。</p>
<p>（2） putenv() 函数并不会为 name&#x3D;value 字符串分配内存。</p>
<p>（3）只能影响到操作进程和子进程，对其父进程没有影响。</p>
<h4 id="5-1-2-使用实例"><a href="#5-1-2-使用实例" class="headerlink" title="5.1.2 使用实例"></a><font size=3>5.1.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="keyword">extern</span> <span class="type">char</span> **environ;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">if</span> (putenv(<span class="string">&quot;FANHUA=12345&quot;</span>))</span><br><span class="line">	&#123;</span><br><span class="line">		perror(<span class="string">&quot;putenv error&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; environ[i] != <span class="literal">NULL</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">puts</span>(environ[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">SHELL=/bin/bash</span><br><span class="line">SESSION_MANAGER=local/vm:@/tmp/.ICE-unix/1237,unix/vm:/tmp/.ICE-unix/1237</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">此处省略......</span></span><br><span class="line">DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus</span><br><span class="line">_=./a.out</span><br><span class="line">OLDPWD=/home/hk/2Sharedfiles/2Linux/01Study/05-LV5</span><br><span class="line">FANHUA=12345</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="5-2-setenv"><a href="#5-2-setenv" class="headerlink" title="5.2 setenv() "></a><font size=3>5.2 setenv() </font></h3><h4 id="5-2-1-函数说明"><a href="#5-2-1-函数说明" class="headerlink" title="5.2.1 函数说明"></a><font size=3>5.2.1 函数说明</font></h4><p>在 linux 下可以使用 man setenv 命令查看该函数的帮助手册</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">setenv</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *name, <span class="type">const</span> <span class="type">char</span> *value, <span class="type">int</span> overwrite)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数用于向进程的环境变量列表中添加一个新的环境变量或修改现有环境变量对应的值。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>name ：字符指针类型，指向 name&#x3D;value 形式的字符串中的名称 name 。</li>
<li>value ：字符指针类型，指向环境变量的值。</li>
<li>overwrite ：若参数 name 标识的环境变量已经存在，在参数 overwrite 为 0 时， setenv() 函数将不改变现有环境变量的值，如果参数 overwrite 的值为非 0 ，若参数 name 标识的环境变量已经存在，则覆盖这个环境变量，若不存在则表示添加新的环境变量。</li>
</ul>
<p><strong>【返回值】</strong>返回值为 int 类型，成功返回 0 ；失败将返回 -1 ，并设置 errno 。</p>
<p><strong>【注意事项】</strong></p>
<p>（1） setenv() 函数为形如 name&#x3D;value 的字符串分配一块内存缓冲区，并将参数 name 和参数 value 所指向的字符串复制到此缓冲区中，以此来创建一个新的环境变量。</p>
<p>（2）此函数既可以用于添加环境变量，也可以用于修改环境变量，相对于 putenv() ，一般更推荐使用 setenv() 函数。</p>
<p>（3） setenv() 函数会为环境变量分配一块内存缓冲区，然后成为进程的一部分；而调用 clearenv() 函数来清空环境变量时没有释放该缓冲区（ clearenv() 调用并不知晓该缓冲区的存在，故而也无法将其释放），反复调用者两个函数的程序，会不断产生内存泄漏。</p>
<p>（4）只能影响到操作进程和子进程，对其父进程没有影响。</p>
<h4 id="5-2-2-使用实例"><a href="#5-2-2-使用实例" class="headerlink" title="5.2.2 使用实例"></a><font size=3>5.2.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> *envValue = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (putenv(<span class="string">&quot;FANHUA=12345&quot;</span>))</span><br><span class="line">	&#123;</span><br><span class="line">		perror(<span class="string">&quot;putenv error&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	envValue = getenv(<span class="string">&quot;FANHUA&quot;</span>);</span><br><span class="line">	<span class="keyword">if</span>(envValue != <span class="literal">NULL</span>)</span><br><span class="line">		<span class="built_in">puts</span>(envValue);</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="built_in">puts</span>(<span class="string">&quot;FANHUA is not exist!&quot;</span>);</span><br><span class="line">    </span><br><span class="line">	setenv(<span class="string">&quot;FANHUA&quot;</span>, <span class="string">&quot;asdfg&quot;</span>, <span class="number">1</span>);</span><br><span class="line">    </span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;========after setenv==========\n&quot;</span>);</span><br><span class="line">	envValue = getenv(<span class="string">&quot;FANHUA&quot;</span>);</span><br><span class="line">	<span class="keyword">if</span>(envValue != <span class="literal">NULL</span>)</span><br><span class="line">		<span class="built_in">puts</span>(envValue);</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="built_in">puts</span>(<span class="string">&quot;FANHUA is not exist!&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">12345</span><br><span class="line">========after setenv==========</span><br><span class="line">asdfg</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="5-3-unsetenv"><a href="#5-3-unsetenv" class="headerlink" title="5.3 unsetenv() "></a><font size=3>5.3 unsetenv() </font></h3><h4 id="5-3-1-函数说明"><a href="#5-3-1-函数说明" class="headerlink" title="5.3.1 函数说明"></a><font size=3>5.3.1 函数说明</font></h4><p>在 linux 下可以使用 man unsetenv 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">unsetenv</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *name)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会从环境变量表中移除参数 name 标识的环境变量。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>name ：字符指针类型，指向 name&#x3D;value 形式的字符串中的名称 name 。</li>
</ul>
<p><strong>【返回值】</strong>返回值为 int 类型，成功返回 0 ；失败将返回非 0 值，并设置 errno 。</p>
<p><strong>【注意事项】</strong>只能影响到操作进程和子进程，对其父进程没有影响。</p>
<h4 id="5-3-2-使用实例"><a href="#5-3-2-使用实例" class="headerlink" title="5.3.2 使用实例"></a><font size=3>5.3.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> *envValue = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (putenv(<span class="string">&quot;FANHUA=12345&quot;</span>))</span><br><span class="line">	&#123;</span><br><span class="line">		perror(<span class="string">&quot;putenv error&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	envValue = getenv(<span class="string">&quot;FANHUA&quot;</span>);</span><br><span class="line">	<span class="keyword">if</span>(envValue != <span class="literal">NULL</span>)</span><br><span class="line">		<span class="built_in">puts</span>(envValue);</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="built_in">puts</span>(<span class="string">&quot;FANHUA is not exist!&quot;</span>);</span><br><span class="line">    </span><br><span class="line">	unsetenv(<span class="string">&quot;FANHUA&quot;</span>);</span><br><span class="line">    </span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;========after unsetenv==========\n&quot;</span>);</span><br><span class="line">	envValue = getenv(<span class="string">&quot;FANHUA&quot;</span>);</span><br><span class="line">	<span class="keyword">if</span>(envValue != <span class="literal">NULL</span>)</span><br><span class="line">		<span class="built_in">puts</span>(envValue);</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="built_in">puts</span>(<span class="string">&quot;FANHUA is not exist!&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">12345</span><br><span class="line">========after unsetenv==========</span><br><span class="line">FANHUA is not exist!</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="5-4-直接添加"><a href="#5-4-直接添加" class="headerlink" title="5.4 直接添加"></a><font size=3>5.4 直接添加</font></h3><h4 id="5-4-1-使用格式"><a href="#5-4-1-使用格式" class="headerlink" title="5.4.1 使用格式"></a><font size=3>5.4.1 使用格式</font></h4><p>还有一种方式，更为简单，可以直接在运行我们写的程序时添加环境变量：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;NAME1=value1 NAME2=value2 ...&gt; ./file_name </span><br></pre></td></tr></table></figure>

<p>即直接在执行程序的命令前添加环境变量，多个变量之间使用空格分隔开。</p>
<h4 id="5-4-2-使用实例"><a href="#5-4-2-使用实例" class="headerlink" title="5.4.2 使用实例"></a><font size=3>5.4.2 使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="type">char</span> **environ;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; environ[i] != <span class="literal">NULL</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">puts</span>(environ[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out</span><br><span class="line">FANHUA1=123 FANHUA2=456 ./a.out # 执行程序</span><br></pre></td></tr></table></figure><p>程序执行后，终端将会显示以下信息：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">FANHUA2=456</span><br><span class="line">FANHUA1=123</span><br><span class="line">SHELL=/bin/bash</span><br><span class="line">SESSION_MANAGER=local/vm:@/tmp/.ICE-unix/1237,unix/vm:/tmp/.ICE-unix/1237</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">此处省略......</span></span><br><span class="line">DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus</span><br><span class="line">OLDPWD=/home/hk/2Sharedfiles/2Linux/01Study/05-LV5</span><br><span class="line">_=./a.out</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h1 id="四、进程的内存布局"><a href="#四、进程的内存布局" class="headerlink" title="四、进程的内存布局"></a><font size=3>四、进程的内存布局</font></h1><p>进程在内存中一种典型的布局方式大概包括以下部分：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20220521065909591.png" alt="image-20220521065909591" style="zoom: 33%;" />

<p>我们可以在终端使用 size file_name 命令来查看一个二进制可执行文件的代码段、数据段、 bss 段的段大小。</p>
<ul>
<li>代码段</li>
</ul>
<p>代码段通常是指用来存放程序执行代码的一块内存区域。这部分区域的大小在程序运行前就已经确定，在代码段中，也有可能包含一些只读的常数变量，例如字符串常量等。代码段具有只读属性，以防止程序由于意外而修改其指令，另外代码段是可以共享的，即使在多个进程间也可同时运行同一段程序。</p>
<ul>
<li>数据段</li>
</ul>
<p>数据段通常是指用来存放程序中已初始化的全局变量和静态变量的一块内存区域。当程序加载到内存中时，从可执行文件中读取这些变量的值。</p>
<ul>
<li>BSS 段</li>
</ul>
<p>BSS(Block Started by Symbol) 段通常是指用来存放程序中未初始化的全局变量和静态变量的一块内存区域。在程序开始执行之前，系统会将本段内所有内存初始化为 0 ，可执行文件并没有为 bss 段变量分配存储空间，在可执行文件中只需记录 bss 段的位置及其所需大小，直到程序运行时，由加载器来分配这一段内存空间。</p>
<ul>
<li>堆（ heap ）</li>
</ul>
<p>堆是用于存放进程运行中被动态分配的内存段，当进程调用 malloc 等函数分配内存时，新分配的内存就被动态添加到堆上（堆被扩张）；当利用 free 等函数释放内存时，被释放的内存从堆中被剔除（堆被缩减）</p>
<ul>
<li>栈( stack )</li>
</ul>
<p>栈又称堆栈， 是用户存放程序临时创建的局部变量，（但不包括 static 声明的变量， static 意味着在数据段中存放变量）。除此以外，在函数被调用时，其参数也会被压入发起调用的进程栈中，并且待到调用结束后，函数的返回值也会被存放回栈中。由于栈的先进后出特点，所以栈特别方便用来保存和恢复调用现场。从这个意义上讲，我们可以把堆栈看成一个寄存、交换临时数据的内存区。</p>
<ul>
<li>进程控制块</li>
</ul>
<p>进程控制块（ Processing Control Block ，简称 PCB ），也被称为任务控制块。是操作系统核心中一种数据结构，主要表示进程状态。它是进程管理和控制的最重要的数据结构，每一个进程都有一个 PCB ，在创建进程时，建立 PCB ，伴随进程运行的全过程，直到进程撤销而撤销。通常包含以下信息：</p>
<table>
    <tr><td align="center" width=175px>进程描述信息</td><td align="left">进程标识符：标识各个进程，每个进程都有一个并且唯一的标识符；<br>用户标识符：进程归属的用户，用户标识符主要为共享和保护服务；</td></tr>
    <tr><td align="center" width=175px>进程控制和管理信息</td><td align="left">进程当前状态，如 new、ready、running、waiting 或 blocked 等；<br>进程优先级：进程抢占 CPU 时的优先级；</td></tr>
    <tr><td align="center" width=175px>资源分配清单</td><td align="left">有关内存地址空间或虚拟地址空间的信息，所打开文件的列表和所使用的 I/O 设备信息。</td></tr>
    <tr><td align="center" width=175px>CPU 相关信息</td><td align="left">CPU中各个寄存器的值，当进程被切换时，CPU 的状态信息都会被保存在相应的 PCB 中，以便进程重新执行时，能从断点处继续执行。</td></tr>
</table>

<p>【说明】在 Linux 中， PCB 控制块的具体实现是 task_struct 结构体，但是我好像没找到这个结构体在哪😅，暂时就了解下好了。</p>
<h1 id="五、进程的类型与状态"><a href="#五、进程的类型与状态" class="headerlink" title="五、进程的类型与状态"></a><font size=3>五、进程的类型与状态</font></h1><h2 id="1-进程的类型"><a href="#1-进程的类型" class="headerlink" title="1. 进程的类型"></a><font size=3>1. 进程的类型</font></h2><p>根据进程的特点，可以分为以下三类：</p>
<ul>
<li>交互进程</li>
</ul>
<p>是由 shell 启动的进程，它既可以在前台运行，也可以在后台运行。交互进程在执行过程中，要求与用户进行交互操作。简单来说就是用户需要给出某些参数或者信息，进程才能继续执行。</p>
<ul>
<li>批处理进程：</li>
</ul>
<p>与 Windows 原来的批处理很类似，是一个进程序列。它和在终端无关，它会被提交到一个作业队列中以便顺序执行。</p>
<ul>
<li>守护进程</li>
</ul>
<p>是执行特定功能或者执行系统相关任务的后台进程。守护进程只是一个特殊的进程，不是内核的组成部分，也与终端无关。许多守护进程在系统启动时启动，直到系统关闭时才停止运行。而某些守护进程只是在需要时才会启动，比如 FTP 或者 Apache 服务等，可以在需要的时候才启动该服务。</p>
<h2 id="2-进程的状态"><a href="#2-进程的状态" class="headerlink" title="2. 进程的状态"></a><font size=3>2. 进程的状态</font></h2><p>在 Linux 下，进程通常有六种状态：</p>
<table>
    <tr><td align="center" width=100px colspan=2>就绪态(Ready)<td align="center" width=50px>R</td><td align="left">指该进程满足被CPU调度的所有条件但此时并没有被调度执行，只要得到CPU就能够直接运行；意味着该进程已经准备好被CPU执行，当一个进程的时间片到达，操作系统调度程序会从就绪态链表中调度一个进程</td></tr>
    <tr><td align="center" width=100px colspan=2>运行态(Running)<td align="center" width=50px>R</td><td align="left">指该进程当前正在被CPU调度运行，处于就绪态的进程得到CPU调度就会进入运行态</td></tr>
    <tr><td align="center" width=100px colspan=2>僵尸态<td align="center" width=50px>Z</td><td align="left">僵尸态进程其实指的就是僵尸进程，指该进程已经结束、但其父进程还未给它"收尸"</td></tr>
    <tr><td align="center" width=50px rowspan=2>等待态</td><td align="center" width=100px>可中断睡眠状态  <td align="center" width=50px>S</td><td align="left">可中断睡眠也称为浅度睡眠，表示睡的不够"死"，还可以被唤醒，一般来说可以通过信号来唤醒</td></tr>
    <tr><td align="center" width=100px>不可中断睡眠状态<td align="center" width=50px>D</td><td align="left">不可中断睡眠称为深度睡眠，深度睡眠无法被信号唤醒，只能等待相应的条件成立才能结束睡眠状态。把浅度睡眠和深度睡眠统称为等待态（或者叫阻塞态），表示进程处于一种等待状态，等待某种条件成立之后便会进入到就绪态；所以，处于等待态的进程是无法参与进程系统调度的</td></tr>
    <tr><td align="center" width=100px colspan=2>暂停态<td align="center" width=50px>T</td><td align="left">暂停并不是进程的终止，表示进程暂停运行，一般可通过信号将进程暂停，譬如SIGSTOP信号；处于暂停态的进程是可以恢复进入到就绪态的，譬如收到SIGCONT信号</td></tr>
</table>
【说明】

<p>（1）很多操作系统的资料会将正在 CPU 上执行的进程定义为 Running 状态、而将可执行但是尚未被调度执行的进程定义为 Ready 状态，这两种状态在 linux 下统一为  TASK_RUNNING 状态。</p>
<p>（2）进程在退出的过程中，处于 TASK_DEAD 状态。在这个退出过程中，进程占有的所有资源将被回收，除了 task_struct 结构（以及少数资源）以外。于是进程就只剩下 task_struct 这么个空壳，故称为僵尸。为啥要保留保留 task_struct 呢，是因为 task_struct 里面保存了进程的退出码、以及一些统计信息，而该进程的父进程很可能会关心这些信息。</p>
<p>（3）进程还有一种死亡状态 X ，也可以说是退出状态，我们使用 ps 命令时几乎不看不到有这样的进程。进程在退出过程中也可能不会保留它的 task_struct ，比如这个进程是多线程程序中被线程分离（后边学习）过的进程，或者父进程通过设置 SIGCHLD 信号的 handler 为 SIG_IGN ，显式的忽略了 SIGCHLD 信号。（这是 posix  的规定，尽管子进程的退出信号可以被设置为 SIGCHLD 以外的其他信号。）此时，进程将被置于 EXIT_DEAD 退出状态，这意味着接下来的代码立即就会将该进程彻底释放，所以 EXIT_DEAD 状态是非常短暂的，几乎不可能通过 ps 命令捕捉到。</p>
<p>（4）进程各状态之间的切换如下图所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20220521105531500.png" alt="image-20220521105531500" style="zoom: 50%;" />

<h1 id="六、进程的虚拟地址"><a href="#六、进程的虚拟地址" class="headerlink" title="六、进程的虚拟地址"></a><font size=3>六、进程的虚拟地址</font></h1><p>在 Linux 系统中，采用了虚拟内存管理技术，事实上大多数现在操作系统都是这样的。 Linux 系统中，每一个进程都在自己独立的地址空间中运行，在 32 位系统中，每个进程的逻辑地址空间均为 4GB ，这 4GB 的内存空间按照 3:1 的比例进行分配，其中用户进程享有 3G 的空间，而内核独自享有剩下的 1G 空间</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20220521112107420.png" alt="image-20220521112107420" style="zoom:33%;" />

<p>虚拟地址会通过硬件 MMU （内存管理单元）映射到实际的物理地址空间中，建立虚拟地址到物理地址的映射关系后，对虚拟地址的读写操作实际上就是对物理地址的读写操作， MMU 会将物理地址翻译为对应的物理地址，其关系如下所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20220521112329080.png" alt="image-20220521112329080" style="zoom: 33%;" />

<p>在 Linux 系统下，应用程序运行在一个虚拟地址空间中，所以程序中读写的内存地址对应也是虚拟地址，并不是真正的物理地址，例如应用程序中读写 0x80800000 这个地址，实际上并不对应于硬件的 0x80800000 这个物理地址。</p>
<h1 id="七、进程的上下文切换"><a href="#七、进程的上下文切换" class="headerlink" title="七、进程的上下文切换"></a><font size=3>七、进程的上下文切换</font></h1><p>各个进程之间是共享  CPU  资源的，在不同的时候进程之间需要切换，让不同的进程可以在  CPU  执行，那么这个<strong>一个进程切换到另一个进程运行，称为进程的上下文切换</strong>。</p>
<h2 id="1-什么是上下文切换？"><a href="#1-什么是上下文切换？" class="headerlink" title="1. 什么是上下文切换？"></a><font size=3>1. 什么是上下文切换？</font></h2><p>大多数操作系统都是多任务的，通常支持大于  CPU  数量的任务同时运行。实际上，这些任务并不是同时运行的，只是因为系统在很短的时间内，让各个任务分别在  CPU  运行，于是就造成<strong>同时运行的错觉</strong>。</p>
<p>任务是交给  CPU  运行的，那么在每个任务运行前， CPU  需要知道任务从哪里加载，又从哪里开始运行。所以，操作系统需要事先帮  CPU  设置好  CPU  寄存器和程序计数器。 CPU  寄存器是  CPU  内部一个容量小，但是速度极快的内存（缓存）。程序计数器则是用来存储  CPU  正在执行的指令位置、或者即将执行的下一条指令位置。所以说， CPU  寄存器和程序计数是  CPU  在运行任何任务前，所必须依赖的环境，这些环境就叫做 <strong>CPU 上下文</strong>。</p>
<p>那什么又是 CPU 上下文切换呢？</p>
<p> CPU  上下文切换就是先把前一个任务的  CPU  上下文（ CPU  寄存器和程序计数器）保存起来，然后加载新任务的上下文到这些寄存器和程序计数器，最后再跳转到程序计数器所指的新位置，运行新任务。系统内核会存储保持下来的上下文信息，当此任务再次被分配给  CPU  运行时， CPU  会重新加载这些上下文，这样就能保证任务原来的状态不受影响，让任务看起来还是连续运行。</p>
<p>上面说到所谓的任务，主要包含进程、线程和中断。所以，可以根据任务的不同，把  CPU  上下文切换分成：<strong>进程上下文切换、线程上下文切换和中断上下文切换</strong>。</p>
<h2 id="2-上下文切换什么？"><a href="#2-上下文切换什么？" class="headerlink" title="2. 上下文切换什么？"></a><font size=3>2. 上下文切换什么？</font></h2><p>进程是由内核管理和调度的，所以进程的切换只能发生在内核态。进程是由内核管理和调度的，所以进程的切换只能发生在内核态。所以，<strong>进程的上下文切换不仅包含了虚拟内存、栈、全局变量等用户空间的资源，还包括了内核堆栈、寄存器等内核空间的资源。</strong></p>
<p>通常，会把交换的信息保存在进程的  PCB ，当要运行另外一个进程的时候，我们需要从这个进程的  PCB  取出上下文，然后恢复到  CPU  中，这使得这个进程可以继续执行，如下图所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-01-%E8%BF%9B%E7%A8%8B-01-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20220523214829930.png" alt="image-20220523214829930" style="zoom:35%;" />

<p>进程的上下文开销是很关键的，我们希望它的开销越小越好，这样可以使得进程可以把更多时间花费在执行程序上，而不是耗费在上下文切换。</p>
<h2 id="3-什么场景会发生？"><a href="#3-什么场景会发生？" class="headerlink" title="3. 什么场景会发生？"></a><font size=3>3. 什么场景会发生？</font></h2><p>（1）为了保证所有进程可以得到公平调度， CPU  时间被划分为一段段的时间片，这些时间片再被轮流分配给各个进程。这样，当某个进程的时间片耗尽了，进程就从运行状态变为就绪状态，系统从就绪队列选择另外一个进程运行；</p>
<p>（2）进程在系统资源不足（比如内存不足）时，要等到资源满足后才可以运行，这个时候进程也会被挂起，并由系统调度其他进程运行；</p>
<p>（3）当进程通过睡眠函数  sleep  这样的方法将自己主动挂起时，也会重新调度；</p>
<p>（4）当有优先级更高的进程运行时，为了保证高优先级进程的运行，当前进程会被挂起，由高优先级进程来运行；</p>
<p>（5）发生硬件中断时， CPU  上的进程会被中断挂起，转而执行内核中的中断服务程序；</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/d2eb46ea.html">LV05-01-进程-01-进程的基础知识</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年07月02日 - 21:51</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/d2eb46ea.html" title="LV05-01-进程-01-进程的基础知识">https://sumumm.github.io/post/d2eb46ea.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag"><i class="fa fa-tag"></i> LV05-操作系统</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/6aea1b71.html" rel="prev" title="LV05-01-进程-02-进程的创建与回收">
                  <i class="fa fa-angle-left"></i> LV05-01-进程-02-进程的创建与回收
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/beeb17ce.html" rel="next" title="LV05-部署云服务器-02-部署网站">
                  LV05-部署云服务器-02-部署网站 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
